package Dynamic_programming;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFDataFormat;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.awt.*;
import java.awt.Font;

/**
 * @Description: $
 * @Param: $
 * @return: $
 * @Author 万家欣
 * @Date: 2022/6/28
 * Algorithm
 * @Version 1.0
 */
public class SheetStyle {

    /**
     * 水平居中、垂直居中
     * 字体：宋体
     * 字体大小：16号
     * 加粗
     *
     * @param workbook
     * @return
     */
    public static CellStyle getStyle(XSSFWorkbook workbook) {
        CellStyle cellstyle = workbook.createCellStyle();
        cellstyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
        cellstyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
        XSSFFont font = workbook.createFont();//字体
        font.setFontName("宋体");//字体
        font.setFontHeightInPoints((short) 18);//字号
        font.setBold(true);//加粗
        cellstyle.setFont(font);
        setBorderStyle(cellstyle);
        return cellstyle;
    }

    /**
     * 获取默认的cell表格样式，加边框，水平居中，垂直居中
     *
     * @param workbook
     * @return
     */
    public static CellStyle getTextCellStyle(XSSFWorkbook workbook) {
        CellStyle style = workbook.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);//水平居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
        setBorderStyle(style);
        return style;
    }

    /**
     * 获取默认的cell表格样式，加边框，水平居中，垂直居中
     *
     * @param workbook
     * @return
     */
    public static CellStyle getNumCellStyle(XSSFWorkbook workbook) {
        CellStyle style = workbook.createCellStyle();
        XSSFDataFormat df = new XSSFWorkbook().createDataFormat();
        style.setAlignment(HorizontalAlignment.RIGHT);//水平居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
        style.setDataFormat(df.getFormat("0.00"));
        XSSFFont font = workbook.createFont();//字体
        font.setFontName("宋体");//字体
        font.setFontHeightInPoints((short) 11);//字号
        style.setFont(font);
        setBorderStyle(style);
        return style;
    }

    /**
     * 边框样式
     *
     * @param style
     */
    public static void setBorderStyle(CellStyle style) {
        style.setBorderBottom(BorderStyle.THIN); //下边框
        style.setBorderLeft(BorderStyle.THIN);//左边框
        style.setBorderTop(BorderStyle.THIN);//上边框
        style.setBorderRight(BorderStyle.THIN);//右边框
    }

    /**
     * 奇数行
     * 背景颜色为黄色
     *
     * @param style
     */
    public static void setCellStyleYellow(CellStyle style) {
        style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    }

    /**
     * 偶数行
     * 背景颜色为LIME
     *
     * @param style
     */
    public static void setCellStyleLime(CellStyle style) {
        style.setFillForegroundColor(IndexedColors.LIME.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    }

    /**
     * 字体设置红色
     *
     * @param workbook
     * @param style
     */
    public static void setFontRedColor(XSSFWorkbook workbook, CellStyle style) {
        XSSFFont font = workbook.createFont();//字体
        font.setColor(IndexedColors.RED.getIndex());
        style.setFont(font);
    }
}
